package com.meij.activity.user;

import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import cn.pedant.SweetAlert.SweetAlertDialog;

import com.anno.ActionBarSet;
import com.anno.ActionBarSet.WAY;
import com.meij.MJApplaction;
import com.meij.R;
import com.meij.activity.MjActivity;
import com.meij.http.Json;
import com.meij.http.MJNetCallBack;
import com.meij.http.Url;
import com.meij.util.Info;

@ActionBarSet(title = "找回密码", way = WAY.TITLE, homeAsUpEnabled = true)
public class FindPwdActivity extends MjActivity {
	// 注册信息相关
	private String phone, code, newpass;
	// 摇晃提示动画
	private Animation shake;
	// 控件
	private EditText edite1, edite2, edite3, edite4;
	private Button button1;
	// 验证码倒计时时间
	private int countDown = 60;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_findpwd);
		initView();
	}

	/***
	 * 初始化UI
	 */
	private void initView() {
		shake = AnimationUtils.loadAnimation(this, R.anim.shake);
		edite1 = (EditText) findViewById(R.id.editText1);
		edite2 = (EditText) findViewById(R.id.editText2);
		edite3 = (EditText) findViewById(R.id.editText3);
		edite4 = (EditText) findViewById(R.id.editText4);
		edite4.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				switch (actionId) {
				case EditorInfo.IME_ACTION_DONE:
					submit(null);
					break;
				}
				return false;
			}
		});
		button1 = (Button) findViewById(R.id.button1);
	}

	/***
	 * 手机号校验
	 * 
	 * @return
	 */
	private boolean checkPhone() {
		phone = edite1.getText().toString();
		if (TextUtils.isEmpty(phone)) {
			Toast(R.string.sign_numhit);
			edite1.requestFocus();
			edite1.startAnimation(shake);
			return false;
		}
		return true;
	}

	/***
	 * 获取验证码
	 * 
	 * @param view
	 */
	public void getCode(View view) {
		if (checkPhone()) {
			String url = Url.vcode + "?mobile=" + phone;
			MJNetCallBack callBack = new MJNetCallBack(this) {
				@Override
				public void onSuccess(JSONObject object) {
					super.onSuccess(object);
					button1.setEnabled(false);
					button1.setText(countDown + "s");
					countDown();
					edite2.requestFocus();
					code = Json.getVcode(object);
				}
			};
			getJson(url, callBack);
		}
	}

	/***
	 * 倒计时
	 */
	private void countDown() {
		button1.postDelayed(new Runnable() {
			@Override
			public void run() {
				if (!isFinishing()) {
					countDown--;
					if (countDown > 0) {
						countDown();
						button1.setText(countDown + "s");
					} else {
						countDown = 60;
						button1.setText(R.string.sign_getcode);
						button1.setEnabled(true);
					}
				}
			}
		}, 1000);
	}

	/***
	 * 其他信息校验
	 * 
	 * @return
	 */
	private boolean checkInfo() {
		code = edite2.getText().toString();
		if (TextUtils.isEmpty(code)) {
			Toast(R.string.sign_codehit);
			edite2.requestFocus();
			edite2.startAnimation(shake);
			return false;
		}
		newpass = edite3.getText().toString();
		if (TextUtils.isEmpty(newpass)) {
			Toast(R.string.change_newpwdhit);
			edite3.requestFocus();
			edite3.startAnimation(shake);
			return false;
		}
		String renewpass = edite4.getText().toString();
		if (TextUtils.isEmpty(renewpass)) {
			Toast(R.string.change_renewpwdhit);
			edite4.requestFocus();
			edite4.startAnimation(shake);
			return false;
		}
		if (!newpass.equals(renewpass)) {
			Toast("新密码与确认新密码不符");
			edite4.requestFocus();
			edite4.startAnimation(shake);
			return false;
		}
		return true;
	}

	/***
	 * 提交数据
	 * 
	 * @param view
	 */
	public void submit(View view) {
		if (checkPhone() && checkInfo()) {
			String url = Url.resetpwdcode + "?mobile=" + phone + "&pwds=" + newpass + "&vcode=" + code;
			MJNetCallBack callBack = new MJNetCallBack(this) {
				@Override
				public void onSuccess(JSONObject object) {
					super.onSuccess(object);
					showAlert();
				}
			};
			getJson(url, callBack);
		}
	}

	/***
	 * 弹出提示
	 */
	private void showAlert() {
		new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE).setTitleText("找回成功").setConfirmText("好的")
				.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
					@Override
					public void onClick(SweetAlertDialog sDialog) {
						login();
						sDialog.dismiss();
					}
				}).show();
	}

	/***
	 * 登录
	 */
	private void login() {
		String url = Url.login + "?mobile=" + phone + "&pwds=" + newpass;
		MJNetCallBack callBack = new MJNetCallBack(this) {
			@Override
			public void onSuccess(JSONObject object) {
				super.onSuccess(object);
				MJApplaction.userInfo = Json.getUserInfo(object);
				Info.saveUserInfo(FindPwdActivity.this);
				Toast("登录成功");
				finish();
			}
		};
		getJson(url, callBack);
	}
}
